package org.wxd.account.domain.user;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * Created by wangxd on 2015/10/6.
 */
@Entity
@Table(name = "t_user")
@Access(AccessType.FIELD)
public class User implements Serializable {

    private static final long serialVersionUID = 4370292633162265838L;

    private static final String ID = "id";
    private static final String IS_LOCK = "is_lock";
    private static final String CREATE_TIME = "create_time";
    private static final String VERSION = "version";
    private static final String MODIFY_TIME = "modify_time";
    private static final String EMAIL = "email";
    private static final String PASSWORD = "password";


    @Id
    @Column(name = ID)
    private String id = UUID.randomUUID().toString();
    @Column(name = CREATE_TIME,updatable = false)
    protected long createTime = new Date().getTime();
    @Column(name = MODIFY_TIME)
    protected long modifyTime = new Date().getTime();
    @Column(name = VERSION)
    protected int version;
    @Column(name = IS_LOCK)
    protected boolean isLock;
    @Column(name = EMAIL)
    private String email;
    @Column(name = PASSWORD)
    private String password;
    @Embedded
    private UserMoreInfo moreInfo;

    User() {
    }

    User(Builder builder) {
        this.id = builder.id;
        this.email = builder.email;
        this.password = builder.password;
        this.moreInfo = builder.moreInfo;
    }

    public User lock(){
        this.isLock = true;
        return this;
    }

    public String id() {
        return id;
    }

    public long createTime() {
        return createTime;
    }

    public long modifyTime() {
        return modifyTime;
    }

    public int version() {
        return version;
    }

    public boolean isLock() {
        return isLock;
    }

    public String email() {
        return email;
    }
    public User email(String email) {
        this.email = email;
        return this;
    }


    public String password() {
        return password;
    }

    public UserMoreInfo moreInfo() {
        return moreInfo;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public UserMoreInfo active() {
        return this.moreInfo().active(true);
    }

    public User updateModifyTime() {
        this.modifyTime = new Date().getTime();
        return this;
    }

    public static class Builder {
        String id;
        long createTime;
        long modifyTime;
        int version;
        boolean isLock;
        String email;
        String password;
        UserMoreInfo moreInfo;

        Builder() {
        }

        public Builder createTime(long createTime) {
            this.createTime = createTime;
            return this;
        }

        public Builder modifyTime(long modifyTime) {
            this.modifyTime = modifyTime;
            return this;
        }

        public Builder version(int version) {
            this.version = version;
            return this;
        }

        public Builder isLock(boolean isLock) {
            this.isLock = isLock;
            return this;
        }

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public Builder email(String email) {
            this.email = email;
            return this;
        }

        public Builder password(String password) {
            this.password = password;
            return this;
        }

        public Builder moreInfo(UserMoreInfo moreInfo) {
            this.moreInfo = moreInfo;
            return this;
        }

        public User build() {
            return new User(this);
        }
    }

}
